//
//  PlayView.m
//  27-Block练习-播放按钮
//
//  Created by 鹏飞罗 on 16/8/19.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
@interface PlayView ()
@property (nonatomic, strong)UIButton *playButton;
@property (nonatomic, strong)UIButton *nextButton;
@property (nonatomic, strong)UIButton *lastButton;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);
@property (nonatomic, copy) void (^next) (UIButton *next);
@property (nonatomic, copy) void (^last) (UIButton *last);
@end
@implementation PlayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void) setUp {
  
    _playButton = [self buttonImageWithName:@"play" action:@selector(play:)];
    
    
    _nextButton = [self buttonImageWithName:@"next" action:@selector(next:)];
    
    _lastButton = [self buttonImageWithName:@"last" action:@selector(last:)];
    
   
}
- (UIButton *)buttonImageWithName:(NSString *)imageName action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button];
    
    return button;
}

- (void)play:(UIButton *)playButton {
    [playButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    
    [playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    
    [playButton addTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
    
    self.play(playButton);
    
}
- (void) pasue:(UIButton *)pasueButton {
    [pasueButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    
    [pasueButton removeTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
    
    [pasueButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(pasueButton);
}
- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
    
}
- (void)next:(void (^)(UIButton *))next last:(void (^)(UIButton *))last {
    self.next = next;
    self.last = last;
}
- (void) next:(UIButton *)next {
    self.next(next);
}
- (void) last:(UIButton *)last {
    self.last(last);
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    
    _lastButton.pf_width = 60;
    _lastButton.pf_height = 60;
    _lastButton.pf_centerY = _playButton.pf_centerY;
  
    _lastButton.pf_centerX = _playButton.pf_centerX - (_lastButton.pf_width / 2 + _playButton.pf_width / 2);
    
    
    
    _nextButton.pf_width = 60;
    _nextButton.pf_height = 60;
    _nextButton.pf_centerY = _playButton.pf_centerY;
    
    _nextButton.pf_centerX = _playButton.pf_centerX + (_lastButton.pf_width / 2 + _playButton.pf_width / 2);
    
}

@end
